package com.AFH.test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

import android.graphics.Canvas;

public class AttackThread  {
	ArrayList<int[]> animationList=new ArrayList<int[]>();
	
	int sleepSpan;
	int attack;
	Sprite sp;
	AttackRunningThread art;
	boolean paused = false;
	Object ob;
	Method method1;
	Method method2;
	Method method3;
	Method method4;
	Method method5;
	protected int currentFrame;
	protected int currentSegment;
	protected float xPos;
	protected float yPos;

	

	public AttackThread(Sprite sp) {
		this.sp = sp;
		currentFrame = 0;
		currentSegment = 0;
		art=new AttackRunningThread(this);
	}

	public void stopAttack() {
		art.flag = false;
		art.spStart = false;
		if(sp.currentSegment!=3) sp.setCurrentSegment(0);
	}

	public boolean started() {
		return (art.flag && art.spStart);

	}

	public void startAttack(int interval, int attack, Object ob) {
		// TODO Auto-generated constructor stub
		sleepSpan = interval;
		this.attack = attack;
		// this.sp=sp;
		art.flag = true;
		art.spStart = true;
		this.ob = ob;
		// ob=Class.forName(o.getClass().getName()).newInstance();

		try {
			method1 = ob.getClass().getMethod("underattack", int.class);
			method2 = ob.getClass().getMethod("isdead");
			method3 = ob.getClass().getMethod("setCurrentSegment", int.class);
			method4 = ob.getClass().getMethod("getxPos");
			method5 = ob.getClass().getMethod("getyPos");

		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	public void startAnimation(){
		art.spStart = true;
		if(!art.isAlive()){
			art.start();
		}
	}


	
	public void drawself(Canvas canvas) {
		try {
			xPos = (Float) method4.invoke(ob);
			yPos = (Float) method5.invoke(ob);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		int ImgIndex = animationList.get(currentSegment)[currentFrame];
		BitmapManager.DrawAttackResource(ImgIndex, canvas, xPos, yPos, null);
	}
	public void makeAnimation(int ani[][]){
		for(int [] imgIds:ani){
			this.animationList.add(imgIds);
		}
	}

	public void pauseAnimation() {
		art.spStart = false;
		paused = true;
	}

	public void resumeAnimation() {
		if (paused) {
			paused = false;
			art.spStart = true;
		}
	}
	
	
	public void nextframe() {
		// TODO Auto-generated method stub
		// if(currentFrame<animationList.get(currentSegment).length-1)
		// currentFrame++;
		currentFrame = (currentFrame + 1)
				% (animationList.get(currentSegment).length);
	}

	
}
